package cn.itcast.mq.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class MqListener {
    @RabbitListener(queues = "simple.queue")
    public void listenerSimpleQueue(String msg){
        System.out.println("listener the simple.queue message :" + msg);
    }

    /**
     *  当包含多个消费者时，未绑定prefect属性，遵循轮询的机制
     *  一个消费者消费一条，当性能不同时，会导致信息的堆积
     */
    @RabbitListener(queues = "work.queue")
    public void listenerWorkQueue1(String msg) throws InterruptedException {
        System.out.println("listener1 the work.queue message_:" + msg);
        Thread.sleep(20);
    }
    @RabbitListener(queues = "work.queue")
    public void listenerWorkQueue2(String msg) throws InterruptedException {
        System.err.println("listener2 the work.queue message++++++:" + msg);
        Thread.sleep(200);
    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenerFanoutQueue1(String msg){
        System.out.println("listener the Fanout.queue1 message_:" + msg);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenerFanoutQueue2(String msg){
        System.err.println("listener the Fanout.queue2 message++++++:" + msg);
    }
    @RabbitListener(queues = "direct.queue1")
    public void listenerDirectQueue1(String msg){
        System.out.println("listener the Fanout.queue1 message_:" + msg);
    }
    @RabbitListener(queues = "direct.queue2")
    public void listenerDirectQueue2(String msg){
        System.err.println("listener the Fanout.queue2 message++++++:" + msg);
    }
    /**
     *      topic交换机：*匹配一个单词
     *                  #匹配0个或多个单词
     *                  消费者匹配多个路由
     */
    @RabbitListener(queues = "topic.queue1")
    public void listenerTopicQueue1(String msg){
        System.out.println("listener the Topic.china.# message_:" + msg);
    }
    @RabbitListener(queues = "topic.queue2")
    public void listenerTopicQueue2(String msg){
        System.err.println("listener the topic.#.weather message++++++:" + msg);
    }
    @RabbitListener(queues = "fanout.queue3")
    public void listenerFanoutQueue3(String msg){
        System.err.println("listener the java write exchange fanout message++++++:" + msg);
    }
    /**
     *      用注解生成交换机，队列，以及绑定
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue3"),
            exchange = @Exchange(name = "hmall.direct2", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
            ))
    public void listenerDirectQueue3(String msg){
        System.err.println("listener the java write exchange fanout message++++++:" + msg + "my keys contain red and blue");
    }
    @RabbitListener(queues = "object.queue")
    public void listenerObjectQueue(Map<String, Object> msg){
        System.out.println("我接收到一个object对象: "+msg);
    }
}
